package com.powerbank.financecenter.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.powerbank.financecenter.entity.WithdrawalRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 提现记录Mapper接口
 */
@Mapper
public interface WithdrawalRecordMapper extends BaseMapper<WithdrawalRecord> {

    /**
     * 根据提现单号查询提现记录
     */
    @Select("SELECT * FROM withdrawal_record WHERE withdrawal_no = #{withdrawalNo} AND deleted = 0")
    WithdrawalRecord findByWithdrawalNo(@Param("withdrawalNo") String withdrawalNo);

    /**
     * 根据用户ID查询提现记录
     */
    @Select("SELECT * FROM withdrawal_record WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_time DESC")
    List<WithdrawalRecord> findByUserId(@Param("userId") Long userId);

    /**
     * 根据状态查询提现记录
     */
    @Select("SELECT * FROM withdrawal_record WHERE status = #{status} AND deleted = 0")
    List<WithdrawalRecord> findByStatus(@Param("status") String status);

    /**
     * 统计用户提现总额
     */
    @Select("SELECT SUM(amount) FROM withdrawal_record WHERE user_id = #{userId} AND status = 'SUCCESS' AND deleted = 0")
    Double sumWithdrawalByUserId(@Param("userId") Long userId);

    /**
     * 统计各状态提现记录数量
     */
    @Select("SELECT status, COUNT(*) as count FROM withdrawal_record WHERE deleted = 0 GROUP BY status")
    List<Object> countByStatus();
}